﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReactionGame
{
    /// <summary>
    /// 根据显示输入按键，记录消耗时间和出错次数
    /// </summary>
    internal class Program
    { //将要显示的按键枚举出来
            enum outputstring
        {
            Up,
            Down,
            Left,
            Right
        }
        
        static void Main(string[] args)
        {
           ConsoleKey input;
            int errornumber = 0;
            TimeSpan TotalSpan=new TimeSpan(0);
           //循环，直至输入enter键
            do
            {
                //清屏
                Console.Clear();
                //总耗时、出错次数
                Console.WriteLine($"总耗时{TotalSpan.TotalSeconds.ToString()}秒，共出错{errornumber.ToString()}次");
                 //随机显示枚举中的字符
                Random rnd = new Random();
                int enumnumber = rnd.Next(0, 4);
                string output = Enum.GetName(typeof(outputstring), enumnumber);
                Console.WriteLine(output);
                //计算从看见到按下按键的反应时间
                TimeSpan start = new TimeSpan(DateTime.Now.Ticks);
                input = Console.ReadKey(true).Key;
                TimeSpan end = new TimeSpan(DateTime.Now.Ticks);
                TimeSpan ReactionSpan = end.Subtract(start).Duration();
                TotalSpan = ReactionSpan.Add(TotalSpan);
                //判断输入是否正确
                switch (output)
                {
                    case "Up":
                        if (input == ConsoleKey.UpArrow)
                            break;
                        else errornumber++;
                        break;
                    case "Down":
                        if (input == ConsoleKey.DownArrow)
                            break;
                        else errornumber++;
                        break;
                    case "Left":
                        if (input == ConsoleKey.LeftArrow)
                            break;
                        else errornumber++;
                        break;
                    case "Right":
                        if (input == ConsoleKey.LeftArrow)
                            break;
                        else errornumber++;
                        break;
                    default:
                        break;
                }
                
            } while (input != ConsoleKey.Enter);
            
           
        }
    
    }
}
